﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.BLL;
using YouHP.Utility;
using YouHP.Model;
using System.Text.RegularExpressions;

namespace YouHP.Pages.User
{
    public class BasicInfo : UserBasePage
    {

        protected UserInfo _User = new UserInfo();
        protected string UserBirthday = "";
        public List<DistrictInfo> mProvinceList = new List<DistrictInfo>();
        public List<DistrictInfo> mCityList = new List<DistrictInfo>();

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;



            if (RequestTools.IsPost()) HandlePost();
            else HandleGet();
        }

        protected void HandleCommon()
        {
            this.AddLocation("个人信息设置", "/User/BasicInfo.aspx");
            this.SetTitle("个人信息");
            mProvinceList = DistrictBLL.Instance.GetDistrictListByParent(0);
            this.AddDatePicker();

            _User = UserBLL.Instance.GetUserInfo(UserData.Id);
            mCityList = DistrictBLL.Instance.GetDistrictListByParent(_User.ProvinceId);
            UserData = _User;
            if (UserData.Birthday.CompareTo(Convert.ToDateTime("1800-01-01")) > 0)
            {
                UserBirthday = UserData.Birthday.ToString("yyyy-MM-dd");
            }
        }

        protected void HandleGet()
        {

        }

        protected void HandlePost()
        {

            if (RequestTools.GetString("description").Length > 500)
            {
                //如果自我介绍超过500...
                AddErrLine("自我介绍不得超过500个字符");
                return;
            }
            if (RequestTools.GetString("interest").Length > 50)
            {
                //如果兴趣超过50...
                AddErrLine("兴趣介绍不得超过50个字符");
                return;
            }

            UserInfo _User = UserBLL.Instance.GetUserInfo(UserData.Id);

            _User.Gender = RequestTools.GetString("sex");

            string birthday = RequestTools.GetString("textboxDate");
            try
            {
                _User.Birthday = Convert.ToDateTime(birthday);
            }
              
            catch
            {
                birthday = "1970-01-01";
                _User.Birthday = Convert.ToDateTime(birthday);
            }
            

            _User.Interests = Utils.HtmlEncode(RequestTools.GetString("interest"));

            _User.Description = Utils.HtmlEncode(RequestTools.GetString("description"));


            if (RequestTools.GetString("mobile").Trim() != "" &&!Regex.IsMatch(RequestTools.GetString("mobile").Trim(), @"^[\d|-]+$"))
            {
                AddErrLine("移动电话号码中含有非法字符");
            }

            if (RequestTools.GetString("phone").Trim() != "" &&
                !Regex.IsMatch(RequestTools.GetString("phone").Trim(), @"^[\d|-]+$"))
            {
                AddErrLine("固定电话号码中含有非法字符");
            }

            _User.MSN = RequestTools.GetString("msn");
            _User.QQ = RequestTools.GetString("qq");
            _User.MobilePhone = RequestTools.GetString("mobile");
            _User.Telephone = RequestTools.GetString("phone");
            _User.ZipCode = RequestTools.GetString("zip");

            _User.ProvinceId = RequestTools.GetFormInt("province", 0);
           
            _User.CityId = RequestTools.GetFormInt("city", 0);

            _User.Address = RequestTools.GetFormString("address");//病人详细地址
            //if (_User.Address == "")
            //{
            //    _User.Address = DistrictBLL.Instance.GetDistrict(_User.ProvinceId).Name + "&nbsp;" + DistrictBLL.Instance.GetDistrict(_User.CityId).Name;
            //}//如果病人不填，则为省份+城市

            UserBLL.Instance.UpdateUserProfile(_User);

            AddMsgLine("修改个人信息完毕");
            SetUrl("/User/BasicInfo.aspx");
            SetMetaRefresh();

        }
    }
}
